package com.mtao.controller.admin;

import com.mtao.SysMenuService;
import com.mtao.SysRoleService;
import com.mtao.SysRoleUserService;
import com.mtao.exception.BadRequestException;
import com.mtao.utils.ThreadLocalUtil;
import com.mtao.entity.Result;
import com.mtao.entity.SysMenu;
import jakarta.annotation.Resource;

import org.springframework.web.bind.annotation.*;

import java.util.*;


@RestController
@RequestMapping("/sys")
public class SysMenuController {

    @Resource
    private SysMenuService sysMenuService;

    @Resource
    private SysRoleUserService sysRoleUserService;

    @Resource
    private SysRoleService sysRoleService;

    /**
     * 获取菜单列表
     *
     * @return
     */
    @PostMapping("/menu/list")
    public Result getMenuTree() {
        List<SysMenu> sysMenus = sysMenuService.getMenuList();
        return Result.success(sysMenus);
    }
    /**
     * 获取菜单列表
     * @return
     */
    @PostMapping("/menu/role/list")
    public Result getRoleMenuTree(){
        try {
            Map<String, Object> map = ThreadLocalUtil.get();
            List<String> roleList = (List<String>) map.get("role");
            String firstRole = roleList.get(0);
            if(firstRole.isEmpty()){
                return Result.error("该用户没有分配角色");
            }
            if(firstRole.equals("admin")){
                return Result.success(sysMenuService.getMenuList());
            }else{
                Integer userId = (Integer) map.get("userId");
                HashMap rolesByUserId = sysRoleUserService.getRolesByUserId(userId);
                List<Integer> rolesIds= (List<Integer>) rolesByUserId.get("rolesIds");
                return Result.success(sysMenuService.getRoleIdMenu(rolesIds));
            }
        } catch (BadRequestException e) {
            return Result.error(e.getMsg());
        }
    }

    /**
     * 添加菜单
     *
     * @Param: [sysMenu]
     * @return: com.mtao.pojo.Result
     * @Author: starao
     * @Date: 2021/11/27
     */
    @PostMapping("/menu/add")
    public Result addMenu(@RequestBody SysMenu sysMenu){
        try {
            if(sysMenu.getParentId()==null){
                sysMenu.setParentId(0);
            }
            sysMenuService.addMenu(sysMenu);
            return Result.success();
        } catch (BadRequestException e) {
            return Result.error(e.getMsg());
        }
    }

    /**
     * @Description: 编辑菜单
     * @Param: [sysMenu]
     * @return: org.springframework.http.ResponseEntity<java.lang.Object>
     * @Author: starao
     * @Date: 2021/11/27
     */
//    @Log("编辑菜单")
    @PostMapping("/menu/edit")
    public Result editMenu(@RequestBody SysMenu sysMenu){
        try {
            Map<String, Object> map = ThreadLocalUtil.get();
            List<String> roleList = (List<String>) map.get("role");
            String firstRole = roleList.get(0);
            if(firstRole.isEmpty()){
                return Result.error("该用户没有分配角色");
            }
            if(firstRole.equals("admin")){
                sysMenuService.editMenu(sysMenu);
                return Result.success();
            }else{
                return Result.error("该用户没有权限");
            }
        } catch (BadRequestException e) {
            return Result.error(e.getMsg());
        }
    }

    /**
     * @Description: 删除菜单
     * @Param: [id]
     * @return: org.springframework.http.ResponseEntity<java.lang.Object>
     * @Author: starao
     * @Date: 2021/11/27
     */
//    @Log("删除菜单")
    @PostMapping("/menu/delete")
    public Result delMenu(@RequestBody SysMenu sysMenu){
        try {
            Map<String, Object> map = ThreadLocalUtil.get();
            List<String> roleList = (List<String>) map.get("role");
            String firstRole = roleList.get(0);
            if(firstRole.isEmpty()){
                return Result.error("该用户没有分配角色");
            }
            if(firstRole.equals("admin")){
                sysMenuService.delMenu(sysMenu.getId());
                return Result.success();
            }else{
                return Result.error("该用户没有权限");
            }
        } catch (BadRequestException e) {
            return Result.error(e.getMsg());
        }
    }

}
